import pathToRegexp from 'path-to-regexp';
import { settlement, coupon, addrAndSelf, addressAll, freightApi, invoiceApi, submitOrder ,getCounponApi, getInVoiceApi, updateOrder, getOneInVoiceApi } from './api';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'order',
    state: {
        list:[],
        addressEntities:[],
        defaultAddress:[],
        invoice:{
            invState: 1,
            invTitle: 3,
            invContentText: '商品明细',
            invContent: '',
            invCompany: "",
            invCode: "",
            invRegAddr: "",
            invRegPhone: "",
            invRegBname: "",
            invRegBaccount:"",
            invId: '',
            titleName: '',
        },
        totalPrice: '',
        invoiceList: [],
        invoiceData: {},
        invoicesTextList: {},
        invoiceTitle: {},

    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },
        changeInvoice(state, { obj }){
            // console.log(state.invoice);
            return { ...state, invoice:{ ...state.invoice, ...obj } };
        },
        invoiceId(state, {obj}) {
            return {...state, invoiceData: {...state.invoiceData, ...obj}};
        },
        invoiceCon(state, {obj}) {
            return {...state, invoicesTextList: {...state.invoicesTextList, ...obj}};
        },
        invoiceTitle(state, {obj}) {
            return {...state, invoiceTitle: {...state.invoiceTitle, ...obj}};
        },
    },
    effects: {
        *settlementEFF({  }, { put, call }) {
            //  Toast.loading();
            const result = yield call(settlement);
            if (result.code == 200) {
                yield put({ type: 'save', data: { list: result.ret.list } });
                yield put({ type:"save", data:{ totalPrice:result.ret.totalPrice} });
                // console.log(result.ret.totalPrice)
                let ids =  result.ret.list.reduce((p,v,i,a)=>{
                    return p+v.cartIds
                },"")
                yield put({ type:"couponEFF", obj:{ cartIds:ids } });
                //Toast.success(result.msg, 1.5);
                return result.ret.list
            } else {
                // yield put(routerRedux.goBack(-3));
            }
        },
        *getcouEFF({}, { put, call }){
            const result = yield call(getCounponApi);
            if (result.code == 200) {
                // console.log(result)
                yield put({ type: 'save', data: {coupons: result} });

                // Toast.success(result.msg, 1.5);
            } else {

            }
            return result;
        },
        *couponEFF({ obj }, { put, call }){
            const result = yield call(coupon, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data: { list: result.ret.list } });
                //Toast.success(result.msg, 1.5);
            } else {
                Toast.fail(result.msg, 1.5);
            }
        },



        *invoicenEFF({ obj }, { put, call }){
            // console.warn(obj, "invoicenEFF --->>>  obj   确定发票");
            yield put({ type: 'invoiceId', obj: { [obj.LzID]: obj.invId } });
            yield put({ type: 'invoiceCon', obj: { [obj.LzID]: obj.contentText } });
            yield put({ type: 'invoiceTitle', obj: { [obj.LzID]: obj.contentTitle } });

            if ( !obj.noGoBack ) {yield put(routerRedux.replace({
                pathname:`/order`
            }))};
            // const result = yield call(invoiceApi, obj);
            // if (result.code == 200) {
            //     Toast.hide();
            //     console.log(result, "result");
            //     yield put({ type: 'changeInvoice', obj: { ...obj  ,...result.ret } });
            //     console.log({[obj.LzID]: {...obj, ...result.ret}}, result.ret, "asd");
            //     yield put({type: "LzInvoice", obj: {[obj.LzID]: {...obj, ...result.ret}}})
            //     yield put(routerRedux.goBack());
            //     //Toast.success(result.msg, 1.5);
            // } else {
            //     Toast.fail(result.msg, 1.5);
            // }
        },

        *addressAllEff({  }, { put, call }){
            const result = yield call(addressAll);
            if (result.code == 200) {
                let { addressEntities=[] } = result.ret;
                let defaultAddress = addressEntities.filter((v,i,a)=>{
                    return v.isDefault==1
                });
                if(defaultAddress.length==0&&addressEntities.length>0){
                    defaultAddress[0] = addressEntities[0]
                }
                if(defaultAddress.length>0){
                    const result = yield call(freightApi, { cityId:defaultAddress[0].cityId });
                }

                yield put({ type: 'save', data: { addressEntities: addressEntities, defaultAddress: defaultAddress } });
            } else {

            }
        },

        *submitOrderEff({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(submitOrder, obj);
            if (result.code == 200) {
                yield put({ type: 'save', data: { orderData: result } });

            } else {

            }
            return result

        },
        // Lz........
        *getInVoiceEFF({obj}, { put, call }) {
            Toast.loading('加载中...', 3);
            console.warn(obj, 'getInVoiceApi数据。。。');
            const result = yield call(getInVoiceApi, obj);
            console.warn("获取发票信息", result);
            if (result.code == 200) {
                yield put({ type: 'save', data:{ invoiceList: result.ret.list} });
                yield put({ type: 'save', data:{ invoice: result.ret.list[0]} });
                Toast.hide();
            } else {

            }
            return result
        },

        *getOneInVoiceEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(getOneInVoiceApi, obj);
            // console.warn("获取发票信息", result);
            if (result.code == 200) {
                yield put({ type: 'save', data:{ invoice: result.ret.list[0]} });
                Toast.hide();
            } else {

            }
            return result
        },

        *updateOrderEFF({obj}, { put, call }) {
            // console.log(obj);
            Toast.loading('加载中...', 3);
            const result = yield call(updateOrder, obj);
            if (result.code == 200) {
                // yield put({ type: 'save', data:{ invoiceList: result.ret.list[0]} });
                console.warn(result, "数据啊。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
                yield put({ type:'settlementEFF' });
                Toast.hide();
            } else {

            }
            return result
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query = {} }) => {
                //console.log(match)
                // pathname.includes('/goodsDetail/')
                if(pathname =='/order'){
                    // const match = pathToRegexp('/goodsDetail/:specId').exec(pathname);
                    dispatch({ type:'settlementEFF' });
                    dispatch({ type:'addressAllEff' });
                }
                if(pathname.includes('/OrderInvoice')){
                    // console.warn(query, 'query 数据。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。');
                    if (query.invId) dispatch({ type:'getOneInVoiceEFF', obj: {invoiceId: query.invId} });
                    else dispatch({ type:'getInVoiceEFF', obj: {isDefault: 1} });
                }
                if(pathname.includes('/selectInvoice')){
                    dispatch({ type:'getInVoiceEFF' });
                }
            });
        },
    },
};
